package com.kevin.Code.String;

/**
 * @author Vinlee Xiao
 * @Classname ValidateIpAddress
 * @Description Leetcode 468. 验证IP地址 中等难度 细节问题较多
 * @Date 2021/11/13 15:32
 * @Version 1.0
 */
public class ValidateIpAddress {

    /**
     * @param queryIP
     * @return
     */
    public String validIPAddress(String queryIP) {

        int len = queryIP.length();
        if (len > 39 || len == 0) {
            return "Neither";
        }
        String res = new String();
        if (queryIP.contains(".")) {
            if (len > 15) {
                return "Neither";
            }

            String[] split = queryIP.split("\\.", -1);
//            System.out.println(split.length);
            res = isIPV4(split);

        } else if (queryIP.contains(":")) {
            //注意加负一的意义 防止最后一个是:,无法分配导空字符串
            String[] split = queryIP.split(":", -1);
            res = isIPV6(split);

        }
        return res;
    }

    /**
     * @param ips
     * @return
     */
    private String isIPV4(String[] ips) {

        if (ips.length != 4) {
            return "Neither";
        }
        for (String s : ips) {

            int len = s.length();

            //1.判断长度问题
            if (len > 3 || len == 0) {
                return "Neither";
            }

            //2.排除前导零
            if (s.charAt(0) == '0' && len != 1) {
                return "Neither";
            }
            int sum = 0;
            char[] chars = s.toCharArray();
            for (char c : chars) {

                if (!Character.isDigit(c)) {
                    return "Neither";
                } else {

                    sum = sum * 10 + (c - '0');
                    if (sum > 255) {
                        return "Neither";
                    }
                }
            }
            if (sum > 255) {
                return "Neither";
            }
        }
        return "IPv4";
    }

    /**
     * @param ips
     * @return
     */
    private String isIPV6(String[] ips) {

        if (ips.length != 8) {
            return "Neither";
        }

        String hexdigits = "0123456789abcdeABCDEF";
        for (String ip : ips) {
            int len = ip.length();

            if (len == 0 || len > 4) {
                return "Neither";
            }

            char[] chars = ip.toCharArray();
            for (char c : chars) {
                if (hexdigits.indexOf(c) == -1) {
                    return "Neither";
                }
            }
        }
        return "IPv6";
    }


    public String validateIPv6(String IP) {
        String[] nums = IP.split(":", -1);
        String hexdigits = "0123456789abcdefABCDEF";
        for (String x : nums) {
            // Validate hexadecimal in range (0, 2**16):
            // 1. at least one and not more than 4 hexdigits in one chunk
            if (x.length() == 0 || x.length() > 4) return "Neither";
            // 2. only hexdigits are allowed: 0-9, a-f, A-F
            for (Character ch : x.toCharArray()) {
                if (hexdigits.indexOf(ch) == -1) return "Neither";
            }
        }
        return "IPv6";
    }

    public static void main(String[] args) {
        ValidateIpAddress validateIpAddress = new ValidateIpAddress();
        String s = validateIpAddress.validIPAddress("1.1.1.1.");
        System.out.println(s);
    }

}
